package com.hjq.demo.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.demo.R;
import com.hjq.demo.app.AppAdapter;
import com.hjq.demo.http.api.ShipmentListApi;
import com.hjq.demo.other.NewConfig;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatTextView;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 出货记录列表
 */
public final class ShipmentRecordAdapter extends AppAdapter<ShipmentListApi.Bean.RowsBean> {
    public  Boolean isSelect = false;
    public  Boolean isAllSelect = false;
    public OnListener onListener;
    public interface OnListener {
        void acbIsSelectListener();
    }
    public ShipmentRecordAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }
    public void setOnListener(OnListener onListener){
        this.onListener = onListener;
    }



    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private AppCompatTextView tvCustomerName;
        private AppCompatTextView tvTradeName;
        private AppCompatTextView tvSpecifications;
        private AppCompatTextView tvQuantity;
        private AppCompatTextView tvUnitPrice;
        private AppCompatTextView tvAmount;
        private AppCompatTextView tvPaid;
        private AppCompatTextView tvUnpaid;
        private AppCompatTextView tvRemarks;
        private AppCompatTextView tvTime;
        private AppCompatCheckBox acbIsSelect;
        private LinearLayout llIsSelect;
        private ViewHolder() {
            super(R.layout.shipment_record_item);
            tvTime = (AppCompatTextView) findViewById(R.id.tv_time);
            tvCustomerName = (AppCompatTextView) findViewById(R.id.tv_customer_name);
            tvTradeName = (AppCompatTextView) findViewById(R.id.tv_Trade_name);
            tvSpecifications = (AppCompatTextView) findViewById(R.id.tv_specifications);
            tvQuantity = (AppCompatTextView) findViewById(R.id.tv_quantity);
            tvUnitPrice = (AppCompatTextView) findViewById(R.id.tv_unit_price);
            tvAmount = (AppCompatTextView) findViewById(R.id.tv_amount);
            tvPaid = (AppCompatTextView) findViewById(R.id.tv_paid);
            tvUnpaid = (AppCompatTextView) findViewById(R.id.tv_unpaid);
            tvRemarks = (AppCompatTextView) findViewById(R.id.tv_remarks);
            acbIsSelect = (AppCompatCheckBox) findViewById(R.id.acb_is_select);
            llIsSelect = (LinearLayout) findViewById(R.id.ll_is_select);
        }




        @Override
        public void onBindView(int position) {
            //判断是否选择
            if (isSelect){
                acbIsSelect.setVisibility(View.VISIBLE);
            }else {
                acbIsSelect.setVisibility(View.GONE);
            }

            tvCustomerName.setText(NewConfig.settingColorSpan("客户名称："+getItem(position).getUserId(),"客户名称："));
            tvTime.setText(NewConfig.settingColorSpan("出货时间："+getItem(position).getCreatedTime(),"出货时间："));
            tvTradeName.setText(NewConfig.settingColorSpan("商品名称："+getItem(position).getTradeName(),"商品名称："));
            if (getItem(position).getSubType() == null){
                getItem(position).setSubType("其他");
            }
            tvSpecifications.setText(NewConfig.settingColorSpan("规格："+getItem(position).getSpecs() + "("+getItem(position).getSubType()+")"  ,"规格："));
            tvQuantity.setText(NewConfig.settingColorSpan("数量："+getItem(position).getNumber(),"数量："));
            tvUnitPrice.setText(NewConfig.settingColorSpan("单价："+getItem(position).getUnitPrice(),"单价："));
            tvAmount.setText(NewConfig.settingColorSpan("金额："+getItem(position).getSumOfMoney(),"金额："));
            tvPaid.setText(NewConfig.settingColorSpan("已付金额："+getItem(position).getAmountPaid(),"已付金额："));
            tvUnpaid.setText(NewConfig.settingColorSpan("未付金额："+getItem(position).getUnpaidAmount(),"未付金额："));
            if (getItem(position).getRemark() == null){
                getItem(position).setRemark("备注为空可以添加备注哟！");
            }
            tvRemarks.setText(NewConfig.settingColorSpan("备注："+getItem(position).getRemark(),"备注："));
//            mTextView.setText(getItem(position));
            if (isSelect){
                llIsSelect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getItem(position).setSelect(!getItem(position).getSelect());
                        onListener.acbIsSelectListener();
                        notifyItemChanged(position);
                    }
                });
            }
            acbIsSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getItem(position).setSelect(!getItem(position).getSelect());
                    onListener.acbIsSelectListener();
                    notifyItemChanged(position);
                }
            });
            if (getItem(position).getSelect() == null){
                getItem(position).setSelect(false);
            }
            acbIsSelect.setChecked(getItem(position).getSelect());
        }
    }
    public void ShipmentRecordAdapterIsSelect(Boolean isSelect) {
        this.isSelect =  isSelect;
       notifyDataSetChanged();
    }
    public void ShipmentRecordAdapterIsAllSelect(Boolean newChooseAll ) {
        this.isAllSelect =  !newChooseAll;
        //判断是否全选
        if (isAllSelect) {
            for (int i = 0; i < getData().size(); i++) {
                getItem(i).setSelect(true);
            }
        }  else{
            for (int i = 0; i < getData().size(); i++) {
                getItem(i).setSelect(false);
            }
        }
        notifyDataSetChanged();
    }
}